package com.mytool.controller;

import com.mytool.model.ConfigParam;
import com.mytool.service.CsvParserService;
import com.opencsv.exceptions.CsvValidationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
@RequestMapping("/csv-to-sql")
@RequiredArgsConstructor
@Slf4j
public class CsvToSqlController {

    private final CsvParserService csvParserService;

    @GetMapping
    public String index() {
        return "csv-to-sql";
    }

    @PostMapping("/convert")
    public String convertCsvToSql(@RequestParam("file") MultipartFile file, Model model) {
        if (file.isEmpty()) {
            model.addAttribute("error", "请选择一个CSV文件上传");
            return "csv-to-sql";
        }

        try {
            // 保存上传的文件
            String tempDir = System.getProperty("java.io.tmpdir");
            Path tempFile = Paths.get(tempDir, file.getOriginalFilename());
            Files.write(tempFile, file.getBytes());

            // 解析CSV文件
            List<ConfigParam> configParams = csvParserService.parseConfigParamCsv(tempFile.toString());
            
            if (configParams.isEmpty()) {
                model.addAttribute("error", "CSV文件格式错误或者没有数据");
                return "csv-to-sql";
            }

            // 生成DDL和DML
            String ddl = csvParserService.generateDdl();
            String dml = csvParserService.generateDml(configParams);

            model.addAttribute("ddl", ddl);
            model.addAttribute("dml", dml);
            model.addAttribute("recordCount", configParams.size());

            // 删除临时文件
            Files.deleteIfExists(tempFile);

            return "csv-to-sql";
        } catch (IOException | CsvValidationException e) {
            log.error("处理CSV文件出错", e);
            model.addAttribute("error", "处理CSV文件时出错: " + e.getMessage());
            return "csv-to-sql";
        }
    }
    
    @GetMapping("/sample")
    public String processSampleFile(Model model) {
        try {
            // 使用示例文件
            String sampleFilePath = "doc/sample/config_param.csv";
            
            // 解析CSV文件
            List<ConfigParam> configParams = csvParserService.parseConfigParamCsv(sampleFilePath);
            
            if (configParams.isEmpty()) {
                model.addAttribute("error", "示例CSV文件格式错误或者没有数据");
                return "csv-to-sql";
            }

            // 生成DDL和DML
            String ddl = csvParserService.generateDdl();
            String dml = csvParserService.generateDml(configParams);

            model.addAttribute("ddl", ddl);
            model.addAttribute("dml", dml);
            model.addAttribute("recordCount", configParams.size());

            return "csv-to-sql";
        } catch (IOException | CsvValidationException e) {
            log.error("处理示例CSV文件出错", e);
            model.addAttribute("error", "处理示例CSV文件时出错: " + e.getMessage());
            return "csv-to-sql";
        }
    }
} 